//
//  SSDKUserDelegateWrapper.m
//  SuperSDK
//
//  Created by wyht－ios－dev on 15/10/8.
//  Copyright © 2015年 WYHT. All rights reserved.
//

#import "SSDKDelegateWrapper.h"
#import "SSDKEnum.h"
#import "SSDKStatusManager.h"
#import "SSDKUserManager.h"
#import "SSDKEventManager.h"
#import "SSDKEventKeys.h"
#import "SuperSDKPluginTools/SSDKPluginLogUtil.h"

@implementation SSDKUserDelegateWrapper

#pragma mark Singleton Method

static SSDKUserDelegateWrapper *userInstance = nil;

+ (id) instance
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        userInstance = [[self alloc] init];
    });
    return userInstance;
}

+ (id<SSDKDelegate>) delegate {
    if (!userInstance) {
        [self instance];
    }
    
    return userInstance.delegate;
}

#pragma mark - Factory Method

+ (void) initSuccess{
    dispatch_async(dispatch_get_main_queue(), ^{
        [[SSDKStatusManager getInstance] setInitSuccessFlag:YES];
        [[self delegate] callbackWithCode:INIT_SUCCESS jsonData:@""];
        [SSDKPluginLogUtil Log:@"SSDK_callBack INIT_SUCCESS"];
    });
}

+ (void) initFail : (SSDKBaseResData *) errorInfo{
    dispatch_async(dispatch_get_main_queue(), ^{
        [[SSDKStatusManager getInstance] setInitSuccessFlag:NO];
        [[self delegate] callbackWithCode:INIT_FAIL jsonData:errorInfo.toJson];
        [SSDKPluginLogUtil Log:[@"SSDK_callBack INIT_FAIL" stringByAppendingString:errorInfo.toJson]];
    });
}

+ (void) loginSuccess : (SSDKBaseResData *) userInfo{
    dispatch_async(dispatch_get_main_queue(), ^{
        [[SSDKStatusManager getInstance] setLoginSuccessFlag:YES];
        [[self delegate] callbackWithCode:LOGIN_SUCCESS jsonData:userInfo.toJson];
        [SSDKPluginLogUtil Log:[@"SSDK_callBack LOGIN_SUCCESS" stringByAppendingString:userInfo.toJson]];
    });
}

+ (void) loginCancel{
    dispatch_async(dispatch_get_main_queue(), ^{
        [[self delegate] callbackWithCode:LOGIN_CANCEL jsonData:@""];
        [SSDKPluginLogUtil Log:@"SSDK_callBack LOGIN_CANCEL"];
    });
}

+ (void) loginFail : (SSDKBaseResData *) errorInfo{
    dispatch_async(dispatch_get_main_queue(), ^{
        [[SSDKStatusManager getInstance] setLoginSuccessFlag:NO];
        [[self delegate] callbackWithCode:LOGIN_FAIL jsonData:errorInfo.toJson];
        [SSDKPluginLogUtil Log:[@"SSDK_callBack LOGIN_FAIL" stringByAppendingString:errorInfo.toJson]];
    });
}

+ (void) logoutSuccess{
    dispatch_async(dispatch_get_main_queue(), ^{
        [[SSDKStatusManager getInstance] setLoginSuccessFlag:NO];
        [[self delegate] callbackWithCode:LOGOUT_SUCCESS jsonData:@""];
        //发送浮标退出的事件
        [SSDKEventManager postNotificationName:EVENT_FLOAT_LOGOUT object:nil];
        [SSDKPluginLogUtil Log:@"SSDK_callBack LOGOUT_SUCCESS"];
    });
    [[SSDKUserManager getInstance] setCurUserInfo:nil];
}

+ (void) platformEnter{
    dispatch_async(dispatch_get_main_queue(), ^{
        [[self delegate] callbackWithCode:PLATFORM_ENTER jsonData:@""];
        [SSDKPluginLogUtil Log:@"SSDK_callBack PLATFORM_ENTER"];
    });
}

+ (void) exit{
    dispatch_async(dispatch_get_main_queue(), ^{
        [[self delegate] callbackWithCode:EXIT_PAGE jsonData:@""];
        [SSDKPluginLogUtil Log:@"SSDK_callBack EXIT_PAGE"];
    });
}

+ (void) antiAddictionQuery : (SSDKBaseResData *) queryInfo{
    dispatch_async(dispatch_get_main_queue(), ^{
        [[self delegate] callbackWithCode:ANTIADDICTION_QUERY jsonData:queryInfo.toJson];
        [SSDKPluginLogUtil Log:[@"SSDK_callBack ANTIADDICTION_QUERY" stringByAppendingString:queryInfo.toJson]];
    });
}

+ (void) realnameRegister : (SSDKBaseResData *) queryInfo{
    dispatch_async(dispatch_get_main_queue(), ^{
        [[self delegate] callbackWithCode:REALNAME_REGISTER jsonData:queryInfo.toJson];
        [SSDKPluginLogUtil Log:[@"SSDK_callBack REALNAME_REGISTER" stringByAppendingString:queryInfo.toJson]];
    });
}

+ (void) accountSwitchSuccess : (SSDKBaseResData *) userInfo{
    dispatch_async(dispatch_get_main_queue(), ^{
        [[SSDKStatusManager getInstance] setLoginSuccessFlag:YES];
        [[self delegate] callbackWithCode:ACCOUNTSWITCH_SUCCESS jsonData:userInfo.toJson];
        [SSDKPluginLogUtil Log:[@"SSDK_callBack ACCOUNTSWITCH_SUCCESS" stringByAppendingString:userInfo.toJson]];
    });
}

+ (void) accountSwitchFail : (SSDKBaseResData *) errorInfo{
    dispatch_async(dispatch_get_main_queue(), ^{
        [[self delegate] callbackWithCode:ACCOUNTSWITCH_FAIL jsonData:errorInfo.toJson];
        [SSDKPluginLogUtil Log:[@"SSDK_callBack ACCOUNTSWITCH_FAIL" stringByAppendingString:errorInfo.toJson]];
    });
}

@end


@implementation SSDKPayDelegateWrapper

#pragma mark Singleton Method

static SSDKPayDelegateWrapper *payInstance = nil;

+ (id) instance
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        payInstance = [[self alloc] init];
    });
    return payInstance;
}

+ (id<SSDKDelegate>) delegate {
    if (!payInstance) {
        [self instance];
    }
    
    return payInstance.delegate;
}

#pragma mark - Factory Method

+ (void) paySuccess : (SSDKBaseResData *) payInfo {
    dispatch_async(dispatch_get_main_queue(), ^{
        [[self delegate] callbackWithCode:PAY_SUCCESS jsonData:payInfo.toJson];
        [[SSDKStatusManager getInstance] setPayingFlag:NO];
        [SSDKPluginLogUtil Log:[@"SSDK_callBack PAY_SUCCESS" stringByAppendingString:payInfo.toJson]];
    });
}

+ (void) payFail : (SSDKBaseResData *) errorInfo {
    dispatch_async(dispatch_get_main_queue(), ^{
        [[self delegate] callbackWithCode:PAY_FAIL jsonData:errorInfo.toJson];
        [[SSDKStatusManager getInstance] setPayingFlag:NO];
        [SSDKPluginLogUtil Log:[@"SSDK_callBack PAY_FAIL" stringByAppendingString:errorInfo.toJson]];
    });
}

+ (void) payCancel {
    dispatch_async(dispatch_get_main_queue(), ^{
        [[self delegate] callbackWithCode:PAY_CANCEL jsonData:@""];
        [[SSDKStatusManager getInstance] setPayingFlag:NO];
        [SSDKPluginLogUtil Log:@"SSDK_callBack PAY_CANCEL"];
    });
}

+ (void) payWaitCheck : (SSDKBaseResData *) payInfo {
    dispatch_async(dispatch_get_main_queue(), ^{
        [[self delegate] callbackWithCode:PAY_WAIT_CHECK jsonData:payInfo.toJson];
        [[SSDKStatusManager getInstance] setPayingFlag:NO];
        [SSDKPluginLogUtil Log:[@"SSDK_callBack PAY_WAIT_CHECK" stringByAppendingString:payInfo.toJson]];
    });
}

@end
